<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../plugins/select2/select2.css" />
    <link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
    <script src="../plugins/select2/select2.min.js" type="text/javascript"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input  >									
									<button class="btn btn-default">查询</button>                                    
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="table">

			                      </tbody>
			                  </table>
                        </div>
                     </div>
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px" >
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input  class="form-control" placeholder="商品类型" id="specName" name="specName"> </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
		      			<select id="brand" multiple style="width: 100%">

						</select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select id="specification" multiple style="width: 100%">

						</select>
		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" class="btn btn-default" title="新增扩展属性" onclick="inser()"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table class="table table-bordered table-striped"  width="800px" id="productTable">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" class="btn btn-default" title="删除" onclick="deletetr(this)"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
							</tbody>
						</table>	
           
		      		</td>
		      	</tr>	
		      	      
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="save()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 修改 -->
  <div class="modal fade" id="editModals" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" >
  	<div class="modal-content">
  		<div class="modal-header">
  			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
  			<h3 id="myModalLabels">商品类型模板编辑</h3>
  		</div>
  		<div class="modal-body">

  			<table class="table table-bordered table-striped"  width="800px" >
  		      	<tr>
  		      		<td>商品类型</td>
  		      		<td><input  class="form-control" placeholder="商品类型" id="specNames" name="specNames"> </td>
  		      	</tr>
  		      	<tr>
  		      		<td>关联品牌</td>
  		      		<td>
  		      			<select id="brands" multiple style="width: 100%">

  						</select>
  		      		</td>
  		      	</tr>
  		      	<tr>
  		      		<td>关联规格</td>
  		      		<td>
  						<select id="specifications" multiple style="width: 100%">

  						</select>
  		      		</td>
  		      	</tr>

  		      	<tr>
  		      		<td>扩展属性</td>
  		      		<td>
  		      			<div class="btn-group">
                               <button type="button" class="btn btn-default" title="新增扩展属性" onclick="inser()"><i class="fa fa-file-o"></i> 新增扩展属性</button>

                          </div>
  						<table class="table table-bordered table-striped"  width="800px" id="productTables">
  							<thead>
  								<tr>
  								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
  									<td>属性名称</td>
  									<td>操作</td>
  								</tr>
  							</thead>
  							<tbody id="tables">

  							</tbody>
  						</table>

  		      		</td>
  		      	</tr>

  			 </table>

  		</div>
  		<div class="modal-footer">
  			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="toupdata()">保存</button>
  			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
  		</div>
  	  </div>
  	</div>
  </div>
    
</body>

</html>


<script>
	$(function () {
		getspecificationList();
		getBrandsList();
		getdata();
	})
	
	function getdata() {
		$.ajax({
			url:"/TbTypeTemplateController/listTemplate",
			type:"post",
			dataType:"json",
			success:function (parameter) {
				if(parameter.code == 100){
					var tr = "";
					var list = parameter.data;
					console.log(list)
					for (var i = 0;i < list.length;i++){
						var jsonSpec = JSON.parse(list[i].specIds);
						var jsonSpec1 = JSON.parse(list[i].brandIds);
						var jsonSpec2 = JSON.parse(list[i].customAttributeItems);
						var specIds = "";
						var brandIds = "";
						var customAttributeItems = "";
						for (var a = 0;a < jsonSpec.length;a++){
							specIds += "," + jsonSpec[a].text
						}
						for (var j1 = 0;j1 < jsonSpec1.length;j1++){
							brandIds += "," + jsonSpec1[j1].text
						}
						for (var j2 = 0;j2 < jsonSpec2.length;j2++){
							customAttributeItems += "," + jsonSpec2[j2].text
						}
						console.log(specIds.substr(1));
						console.log(brandIds.substr(1));
						console.log(customAttributeItems.substr(1));
						/*拼接*/
						tr += "<tr>";
						tr += "<td>"
						tr += '<input  type="checkbox">'
						tr += "</td>"
						tr += '<td>'+list[i].id+'</td>'
						tr += '<td>'+list[i].name+'</td>'
						tr += '<td>'+brandIds.substr(1)+'</td>'
						tr += '<td>'+specIds.substr(1)+'</td>'
						tr += '<td>'+customAttributeItems.substr(1)+'</td>'
						tr += '<td class="text-center">'
						tr += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModals" onclick="update(' + list[i].id + ')">修改</button>'
						tr += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" onclick="deletes(' + list[i].id + ')">删除</button>'
						tr += '</td>'
						tr += "</tr>";
					}
					$("#table").html(tr);
				}
			},
			error:function () {
				alert("代码错误")
			}
		})
	}

	/*增加扩展属性*/
	function inser() {
	var table = document.getElementById("productTable");
	var tr = document.createElement("tr");
	tr.innerHTML = '<tr><td><input type="checkbox" class="icheckbox_square-blue" ></td><td><input class="form-control" placeholder="属性名称" ></td><td><button type="button" class="btn btn-default" title="删除" onclick="deletetr(this)"><i class="fa fa-trash-o"></i> 删除</button></td></tr>';
	table.appendChild(tr);
	}
	/*删除扩展属性*/
function deletetr(Obj) {
var tr = Obj.parentNode.parentNode;
tr.parentNode.removeChild(tr);
}

	function getBrandsList() {
		$.ajax({
			url:"/ToShow/selectAll",
			type:"post",
			dataType:"json",
			success:function (parameter) {
				console.log(parameter)
				if(parameter.code == 100){
					var option = "";
					var list = parameter.data;
					for (var i = 0;i < list.length; i++){
						option += '<option value="'+list[i].id+'">'
						option += list[i].name;
						option += '</option >'
					}
					$("#brand").html(option);
					$("#brand").select2();
					$("#brands").html(option);
					$("#brands").select2();
				}
			},
			error:function () {
				alert("代码错误")
			}
		})
	}

	function getspecificationList() {
		$.ajax({
			url:"/SpecificationController/selectAll",
			type:"post",
			dataType:"json",
			success:function (tablea) {
				var list = tablea.data
					var option = "";
					for (var i = 0;i < list.length; i++){
						option += '<option value="'+list[i].id+'">'
						option += list[i].specName;
						option += '</option >'
					}
					$("#specification").html(option);
					$("#specification").select2();
				$("#specifications").html(option);
				$("#specifications").select2();
			},
			error:function () {
				alert("代码错误")
			}
		})
	}

	/*zengjia*/
	function save() {
		var brands = $("#brand").select2('data');
		var specifications = $("#specification").select2('data');
		var tab = $("#productTable").find("tr");
		var arr= [];
		var arrs = [];
		var arrss = [];
		$.each(brands,function (i,e) {
			var json = {"id":e.id,"text":e.text}
			arr.push(json);
		})
		$.each(specifications,function (i,e) {
			var json = {"id":e.id,"text":e.text}
			arrs.push(json);
		})
		for(var i = 1;i < tab.length;i++){
			var tds = $(tab[i]).find("td");
			var optionName = $(tds[1]).find("input").val();
			arrss.push({"text":optionName})
		}
		//属性
		var optionsListJson = JSON.stringify(arrss)
		//品牌
		var arrJson = JSON.stringify(arr);
		//规格
		var arrJsons = JSON.stringify(arrs);
		//商品类型
		var specNames = $("#specName").val();

		$.ajax({
			url:"/TbTypeTemplateController/toAdd",
			type:"post",
			data: {name:specNames,specIds:arrJsons,brandIds:arrJson,customAttributeItems:optionsListJson},
			dataType:"json",
			success:function (list) {
					alert("增加成功")
			},
			error:function () {
				alert("代码错误")
			}
		})
	}

	/*删除*/
	function deletes(id) {
		alert("删除方法"+id);
   		$.ajax({
   		    url:"/TbTypeTemplateController/delete?id="+id,
   		    type:"post",
   		    dataType:"json",
   		    success:function (parameter) {
   		        if(parameter){
   		            alert("删除成功")
			  getBrand();
   		        }else{
   		            alert("删除错误")
   		        }
   		    },
   		    error:function () {
   		        alert("代码错误")
   		    }
   		})
	}

	function update(id) {
		alert("修改方法"+id);
   		$.ajax({
   		    url:"/TbTypeTemplateController/updata?id="+id,
   		    type:"post",
   		    dataType:"json",
   		    success:function (parameter) {
				var template = parameter.data;
				alert(template.name)
				$("#specNames").val(template.name)
				var obj = JSON.parse(template.specIds);
				var objs = JSON.parse(template.brandIds);
				/*规格*/
				var ids = [];
				/*品牌*/
				var idss = [];
				/*规格*/
				$.each(obj,function (i,e) {
					ids.push(e.id)
				})
				$("#specifications").val(ids).trigger("change");
				/*品牌*/
				$.each(objs,function (i,e) {
					idss.push(e.id)
				})
				$("#brands").val(idss).trigger("change");

				/*属性回显*/
				var option = JSON.parse(template.customAttributeItems);
				var tr = "";
				for (var i = 0; i < option.length; i++){
					tr += "<tr>";
					tr += '<td><input type="checkbox" value="' + template.id + '" id="ids" name="ids"></td>';
					tr += "<td>";
					tr += "<input type='text' class='form-control' name='optionName' value='" + option[i].text + "' placeholder='属性名称'>";
					tr += "</td>";
					tr += "<td>";
					tr += "<button type='button'  class='btn btn-default' title='删除' onclick='deletetr(this)'><i class='fa fa-trash-o'></i>删除";
					tr += "</button>";
					tr += "</td>";
					tr += "</tr>";
				}
				$("#tables").html(tr);
   		    },
   		    error:function () {
   		        alert("代码错误")
   		    }
   		})
	}
	
	function toupdata() {
		var brands = $("#brands").select2('data');
		var specifications = $("#specifications").select2('data');
		var tab = $("#productTables").find("tr");
		var arr= [];
		var arrs = [];
		var arrss = [];
		$.each(brands,function (i,e) {
			var json = {"id":e.id,"text":e.text}
			arr.push(json);
		})
		$.each(specifications,function (i,e) {
			var json = {"id":e.id,"text":e.text}
			arrs.push(json);
		})
		for(var i = 1;i < tab.length;i++){
			var tds = $(tab[i]).find("td");
			var optionName = $(tds[1]).find("input").val();
			arrss.push({"text":optionName})
		}
		//属性
		var optionsListJson = JSON.stringify(arrss)
		//品牌
		var arrJson = JSON.stringify(arr);
		//规格
		var arrJsons = JSON.stringify(arrs);
		//商品类型
		var specNames = $("#specNames").val();

		var idss = $("#ids").val();

		$.ajax({
			url:"/TbTypeTemplateController/toupdata",
			type:"post",
			data: {id:idss,name:specNames,specIds:arrJsons,brandIds:arrJson,customAttributeItems:optionsListJson},
			dataType:"json",
			success:function (list) {
					alert("增加成功")
			},
			error:function () {
				alert("代码错误")
			}
		})
	}
</script>